package com.android.kmlmrg.listener;

import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.android.kmlmrg.R;
import com.android.kmlmrg.services.ServiceController;
import com.android.kmlmrg.views.DashBoard;

/**
 * 
 * @author mayki
 *
 */
public class FormAuthenticationButtonListener implements OnClickListener{

	/**LOG**/
	private static final  String LOG_TAG = FormAuthenticationButtonListener.class.getName();


	private int vComposantId ;
	private Application vApplication;
	private Activity vContext;

	/**
	 *  
	 * @param pContext pContext
	 * @param pIntent pIntent
	 * @param pComposantId pComposantId
	 */
	public  FormAuthenticationButtonListener(Application pApplication , int pComposantId, Activity activity ){
		Log.i(LOG_TAG, "START - FormAuthenticationButtonListener(Context pContext ,Intent pIntent , int pComposantId ) ");
		vApplication = pApplication;
		vComposantId = pComposantId;
		vContext = activity;
		Log.i(LOG_TAG, "END - FormAuthenticationButtonListener");
	}
	/**
	 * 
	 */
	public void onClick(View pView ) {
		Log.i(LOG_TAG, "START - onClick(View pView )");
		switch (vComposantId) {
		case R.id.btnConnexion:
			actionClickBtnConnexion(pView);
			break;

		default:
			break;
		}
		Log.i(LOG_TAG, "END - onClick(View pView )");

	}


	/**
	 * 
	 * @param pView
	 */
	public void actionClickBtnConnexion( View pView ){

		Log.i(LOG_TAG, "START - actionClickBtnConnexion( View pView )");

		EditText vLogin = (EditText) vContext.findViewById(R.id.UserNameInputText);

		EditText vPassword = (EditText) vContext.findViewById(R.id.PasswordInputText);

		String vLoginVal = vLogin.getText().toString();

		String vPassordVal = vPassword.getText().toString();

		ServiceController vController = ServiceController.getInstance();
		if ( ServiceController.isAuthentication() ){
			if ( vLoginVal.trim().equalsIgnoreCase( vController.getLogin().trim() ) && vPassordVal.equalsIgnoreCase(vController.getPassword().trim() ) ) {

				Intent vIntent = new Intent(vContext, DashBoard.class);
				vIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				vApplication.startActivity(vIntent);
			}else {
				Toast.makeText(vContext, "erreur login/password", Toast.LENGTH_SHORT).show();
			}
		} else {
			ProgressDialog progress = new ProgressDialog(vContext);
			progress.setMessage("Loading...");
			vController.login(vLoginVal, vPassordVal, true);
		}
		Log.i(LOG_TAG, "END - actionClickBtnConnexion( View pView )");
	}

}
